package handler

import (
	"cashcenter/internal/model"
	"cashcenter/internal/service"
	"cashcenter/utils/errx"
	"cashcenter/utils/resx"
	"context"
)

type CashService struct {
	service.UnimplementedCashServiceServer
}

func (e *CashService) CashAdd(ctx context.Context, req *service.CashChangeRequest) (*service.CashChangeResponse, error) {
	accountId, err := model.InsertAccount(req.UserId, req.Count)
	if err != nil {
		return &service.CashChangeResponse{Code: resx.Fail, AccountId: -1}, err
	}
	return &service.CashChangeResponse{Code: resx.Success, AccountId: accountId}, nil
}

func (e *CashService) CashDec(ctx context.Context, req *service.CashChangeRequest) (*service.CashChangeResponse, error) {
	accountId, err := model.InsertAccount(req.UserId, -req.Count)
	if err != nil {
		if err == errx.CtpError(errx.DecError) {
			return &service.CashChangeResponse{Code: resx.Fail, AccountId: -1}, nil
		}
		return &service.CashChangeResponse{Code: resx.SystemErr, AccountId: -1}, err
	}
	return &service.CashChangeResponse{Code: resx.Success, AccountId: accountId}, nil
}
